package com.linkhub.oms.data.sync.service.impl;

import com.linkhub.oms.common.constant.WmsInfoConstant;
import com.linkhub.oms.data.sync.api.wms.client.WmsClient;
import com.linkhub.oms.data.sync.api.wms.domain.dto.*;
import com.linkhub.oms.data.sync.api.wms.domain.request.*;
import com.linkhub.oms.data.sync.api.wms.domain.response.*;
import com.linkhub.oms.data.sync.service.WMSApiService;
import com.linkhub.oms.data.sync.util.SignUtil;
import com.linkhub.oms.data.sync.util.WmsEntryOrderAssist;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import static com.linkhub.oms.data.sync.api.wms.constant.WmsApiConstant.*;

/**
 * @Description: WMSApiService
 * @Author: echo
 * @Date: 2024/5/6 15:47
 * @Version: 1.0
 */
@Slf4j
@Service
public class WMSApiServiceImpl implements WMSApiService {

    @Value("${wms.appKey}")
    private String appKey;

    @Value("${wms.customerId}")
    private String customerId;

    @Value("${wms.secretKey}")
    private String secretKey;

    @Resource
    private WmsClient wmsClient;

    private static final int MAX_RETRIES = 3;

    @Override
    public EntryOrderCreateResponse createEntryOrder( WmsEntryOrderDTO wmsEntryOrderDTO ) {

        WmsBaseURLRequest baseURLRequest = WmsBaseURLRequest.createSimpleRequest(OPENAPI_ENTRY_ORDER_CREATE_METHOD,
                appKey,
                customerId);
        System.out.println(baseURLRequest);
        EntryOrderCreateRequest request = WmsEntryOrderAssist.convertEntryOrderRequest(wmsEntryOrderDTO);
        for (int attempt = 1; attempt <= MAX_RETRIES; attempt++) {
            try {
                return wmsClient.createEntryOrder(baseURLRequest,
                                SignUtil.createWmsSign(request, baseURLRequest, secretKey),
                                request)
                        .getResult();
            } catch (Exception e) {
                log.error("Failed to create entry order, attempt {} of {}", attempt, MAX_RETRIES, e);
                if (attempt == MAX_RETRIES) {
                    throw e;
                }
            }
        }
        return null;
    }

    //同步新增本地商品到仓库
    @Override
    public SyncProductResponse syncAddProduct( WmsSyncProductDTO wmsSyncProductDTO ) {
        wmsSyncProductDTO.setActionType(WmsInfoConstant.SYNC_ADD_PRODUCT);

        return syncProduct(wmsSyncProductDTO);
    }

    //同步新增本地商品到仓库
    @Override
    public SyncProductResponse syncUpdateProduct( WmsSyncProductDTO wmsSyncProductDTO ) {
        wmsSyncProductDTO.setActionType(WmsInfoConstant.SYNC_UPDATE_PRODUCT);
        return syncProduct(wmsSyncProductDTO);

    }

    @Override
    public SyncProductResponse syncProduct( WmsSyncProductDTO wmsSyncProductDTO ) {
        WmsBaseURLRequest baseURLRequest = WmsBaseURLRequest.createSimpleRequest(OPENAPI_SINGLE_ITEM_SYNCHRONIZE_METHOD,
                appKey,
                customerId);
        System.out.println(baseURLRequest);
        SyncProductRequest request = WmsEntryOrderAssist.convertSyncProductRequest(wmsSyncProductDTO);
        for (int attempt = 1; attempt <= MAX_RETRIES; attempt++) {
            try {
                return wmsClient.syncProduct(baseURLRequest,
                                SignUtil.createWmsSign(request, baseURLRequest, secretKey),
                                request)
                        .getResult();
            } catch (Exception e) {
                log.error("Failed to sync product, attempt {} of {}", attempt, MAX_RETRIES, e);
                if (attempt == MAX_RETRIES) {
                    throw e;
                }
            }
        }
        return null;
    }

    //批量创建入库单
    @Override
    public EntryOrderBatchCreateResponse batchCreateEntryOrder( WmsEntryOrderBatchCreateDTO wmsEntryOrderBatchCreateDTO ) {
        WmsBaseURLRequest baseURLRequest = WmsBaseURLRequest.createSimpleRequest(OPENAPI_ENTRY_ORDER_BATCH_CREATE_METHOD,
                appKey,
                customerId);
        System.out.println(baseURLRequest);
        EntryOrderBatchCreateRequest request = WmsEntryOrderAssist.convertEntryOrderBatchCreateRequest(wmsEntryOrderBatchCreateDTO);
        for (int attempt = 1; attempt <= MAX_RETRIES; attempt++) {
            try {
                return wmsClient.batchCreateEntryOrder(baseURLRequest,
                                SignUtil.createWmsSign(request, baseURLRequest, secretKey),
                                request)
                        .getResult();
            } catch (Exception e) {
                log.error("Failed to batch create entry order, attempt {} of {}", attempt, MAX_RETRIES, e);
                if (attempt == MAX_RETRIES) {
                    throw e;
                }
            }
        }
        return null;
    }

    // 批量创建出库单
    @Override
    public StockOutBatchCreateResponse batchCreateStockOutList( WmsStockOutBatchCreateDTO wmsCreateDeliveryListDTO ) {
        WmsBaseURLRequest baseURLRequest = WmsBaseURLRequest.createSimpleRequest(OPENAPI_STOCK_OUT_BATCH_CREATE_METHOD,
                appKey,
                customerId);
        System.out.println(baseURLRequest);
        StockOutBatchCreateRequest request = WmsEntryOrderAssist.convertStockOutBatchCreateRequest(wmsCreateDeliveryListDTO);
        for (int attempt = 1; attempt <= MAX_RETRIES; attempt++) {
            try {
                return wmsClient.batchCreateStockOutList(baseURLRequest,
                                SignUtil.createWmsSign(request, baseURLRequest, secretKey),
                                request)
                        .getResult();
            } catch (Exception e) {
                log.error("Failed to batch create stock out list, attempt {} of {}", attempt, MAX_RETRIES, e);
                if (attempt == MAX_RETRIES) {
                    throw e;
                }
            }
        }
        return null;
    }

    // 批量取消订单
    @Override
    public CancelOrderResponse batchCancelOrder( WmsCancelOrderDTO wmsCancelOrderDTO ) {
        WmsBaseURLRequest baseURLRequest = WmsBaseURLRequest.createSimpleRequest(OPENAPI_ORDER_BATCH_CANCEL_V2_METHOD,
                appKey,
                customerId);
        System.out.println(baseURLRequest);
        BatchCancelOrderRequest request = WmsEntryOrderAssist.convertBatchCancelOrderRequest(wmsCancelOrderDTO);
        for (int attempt = 1; attempt <= MAX_RETRIES; attempt++) {
            try {
                return wmsClient.batchCancelOrderOrder(baseURLRequest,
                                SignUtil.createWmsSign(request, baseURLRequest, secretKey),
                                request)
                        .getResult();
            } catch (Exception e) {
                log.error("Failed to batch cancel order, attempt {} of {}", attempt, MAX_RETRIES, e);
                if (attempt == MAX_RETRIES) {
                    throw e;
                }
            }
        }
        return null;
    }

    // 加工单创建
    @Override
    public StoreProcessCreateResponse createStoreProcess( WmsProcessOrderCreateDTO wmsProcessOrderCreateDTO ) {
        WmsBaseURLRequest baseURLRequest = WmsBaseURLRequest.createSimpleRequest(OPENAPI_STORE_PROCESS_CREATE_METHOD,
                appKey,
                customerId);
        System.out.println(baseURLRequest);
        StoreProcessCreateRequest request = WmsEntryOrderAssist.convertCreateStoreProcessRequest(wmsProcessOrderCreateDTO);
        for (int attempt = 1; attempt <= MAX_RETRIES; attempt++) {
            try {
                return wmsClient.createStoreProcess(baseURLRequest,
                                SignUtil.createWmsSign(request, baseURLRequest, secretKey),
                                request)
                        .getResult();
            } catch (Exception e) {
                log.error("Failed to create store process, attempt {} of {}", attempt, MAX_RETRIES, e);
                if (attempt == MAX_RETRIES) {
                    throw e;
                }
            }
        }
        return null;
    }
}
